/**
 * @author songxz
 * @date 2022/11/11
 */
public class SynchronizedDemo {

    public static int i=1;

    public void method(String name) {
        synchronized (this){
            //业务代码
            System.out.println(name+"修饰实例方法"+i++);
        }
    }
    public RunnableDemo getRunnableDemo(){
        return new RunnableDemo();
    }
    class RunnableDemo implements Runnable{

        /**
         * When an object implementing interface <code>Runnable</code> is used
         * to create a thread, starting the thread causes the object's
         * <code>run</code> method to be called in that separately executing
         * thread.
         * <p>
         * The general contract of the method <code>run</code> is that it may
         * take any action whatsoever.
         *
         * @see Thread#run()
         */
        @Override
        public void run() {
            System.out.println("测试");
            method("线程");
        }
    }
    public static void main(String[] args) {
        SynchronizedDemo synchronizedDemo =new SynchronizedDemo();
        for (int j = 0; j < 5; j++) {
            new Thread(synchronizedDemo.getRunnableDemo()).start();
        }
    }

}
